<?php

require_once 'const/DBConst.php';

class Application_Model_CartMapper {

    public static function getCarts($session) {
        if (!isset($session->carts)) {
            $session->carts = array();
        }
        return $session->carts;
    }

    public static function addToCarts($productID, $session) {
        $product = Application_Model_ProductMapper::fetch($productID);
        $productName = $product[C_NAME];
        $price = $product[C_PRICE];
        $cart = new Application_Model_Cart($productID, $productName, 1, $price);
        if (!isset($session->carts)) {
            $session->carts = array();
        }
        $carts = $session->carts;
        foreach ($carts as $_cart) {
            if ($_cart->getProductID() == $productID) {
                $_cart->setNumber($_cart->getNumber() + 1);
                return;
            }
        }
        array_push($session->carts, $cart);
    }

}

